package vn.edu.ptithcm.pim.validator;

import org.apache.commons.lang.StringUtils;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import vn.edu.ptithcm.pim.dto.ChangePasswordDto;

public class ChangePasswordValidator implements Validator {

	public boolean supports(@SuppressWarnings("rawtypes") Class clazz) {
		return ChangePasswordDto.class.isAssignableFrom(clazz);
	}

	public void validate(Object target, Errors errors) {
		ChangePasswordDto changePasswordDto = (ChangePasswordDto) target;
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "currentPassword",
				"warning.required.password.current");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "newPassword1",
				"warning.required.password.new1");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "newPassword2",
				"warning.required.password.new2");

		if (!StringUtils.equals(changePasswordDto.getNewPassword1(),
				changePasswordDto.getNewPassword2())) {
			errors.reject("newPassword2", "warning.password.new.not.match");
		}
	}
}
